/*
 * Copyright (c) jiucheng.org
 */
package org.jiucheng.template.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author jiucheng
 *
 */
public class Rgxs {
    private static final String regex = "\\$\\{([\"\\-/,\\$\\(\\):a-zA-Z0-9_\\.\\s]+)\\}";
    private static final Pattern pattern = Pattern.compile(regex);
    public static String[] complier(String print, StringBuilder sb) {
        Matcher m = pattern.matcher(print);
        int i = 0;
        int e = 0;
        List<String> args = new ArrayList<String>();
        while(m.find()) {
            args.add(m.group(1).trim());
            if(e == 0) {
                sb.append(print.substring(0, m.start()));
                sb.append("{").append(i).append("}");
            }else {
                sb.append(print.substring(e, m.start()));
                sb.append("{").append(i).append("}");
            }
            e = m.end();
            i++;
        }
        if(args.size() == 0) {
            sb.append(print);
            return null;
        }
        if(e < print.length()) {
            sb.append(print.substring(e));
        }
        String[] a = new String[args.size()];
        args.toArray(a);
        return a;
    }
}
